using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WinCANTool
{
     
    public partial class MDIWindow : Form
    {
        public NewMessage GetCurrentMessage = new NewMessage();
        public CanToolOptionData CurrentOptions = new CanToolOptionData();
        public System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MDIWindow));

        public ParseSymbols CurrentSymbols = new ParseSymbols();
        //public FlashWindow1319 FlashSwWindow;

        public NetNodeFind NodeDisplay;
        public string NodeFindResults = "";
        public string CurrentPath = "";
        public string FileName;
        public int CaptureCount = 1000;

        public event NewMessageHandler DataReady;
        public event NewMDIOptionsHandler DisplayOption;
        public event EventHandler ResetRxTxWindow;

        public FlashWindow1319 newMDIChild1;

        public MDIWindow()
        {
            InitializeComponent();          
            CurrentPath = Directory.GetCurrentDirectory();
            GetOptions();
            //FactoryUserInit(true);
            NetInit();
            InitializeLocalSystemTime();
            NodeDisplay = new NetNodeFind(this);
            SetupStatusLabel(false);
            newMDIChild1 = new FlashWindow1319(this, CurrentOptions);
            newMDIChild1.MdiParent = this;
            newMDIChild1.Show();
            //SymbolFileName.Text = CurrentOptions.FileName;
            FileName = CurrentPath + @"\Symbols\" + SymbolFileName.Text;
            NodesConnect();
            CurrentSymbols.NewSymbols += new EventHandler(LoadSymbols);
        }
        
        
        public void MDIWindow_OptionsReady(object CurrentForm, NewOptionsArgs Information)
        {
            //CurrentOptions.ShowRawData = Information.Options.ShowRawData;
            //CurrentOptions.HexDisplay = Information.Options.HexDisplay;
            //CurrentOptions.ErrorFrames = Information.Options.ErrorFrames;
            //CurrentOptions.ShowPeriod = Information.Options.ShowPeriod;
            //CurrentOptions.CanOpenOn = Information.Options.CanOpenOn;
            //if (CurrentOptions.ErrorFrames)
            //    TurnOnErrorFrames(true);
            //else
            //    TurnOnErrorFrames(false);
            
            ////if (CurrentOptions.ShowPeriod)
            ////    newMDIChild1.dataGridView1.Columns[3].HeaderText = "Period";
            ////else
            ////    newMDIChild1.dataGridView1.Columns[3].HeaderText = "Count";

            //for (int i = 0; i < 8; i++)
            //    CurrentOptions.DataChange[i] = Information.Options.DataChange[i];

            //OptionDataReady(Information);
        }


        
        
        protected virtual void MessageDataReady(NewEventArgs e)
        {

            if (DataReady!= null)
                DataReady(this, e);
        }

        protected virtual void OptionDataReady(NewOptionsArgs e)
        {

            if (DisplayOption != null)
                DisplayOption(this, e);
        }



        public void SetupStatusLabel(bool NetConnectOn)
        {
            if (NetConnectOn)
            {
                toolStripStatusLabel1.Text = "Net Connected";
                toolStripStatusLabel1.Image = imageList1.Images[0];
            
            }
            else
            {
                toolStripStatusLabel1.Text = "Net Not Connected";
                toolStripStatusLabel1.Image = imageList1.Images[1];

            }
        }




        private void txRxWindowToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ResetRxTxWindow != null)
                ResetRxTxWindow(this, e);
        }


        private void newMessageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            EnterMessageWindow newMDIChild4 = new EnterMessageWindow();
            newMDIChild4.MdiParent = this;
            newMDIChild4.NewData += new NewMessageHandler(passToDisplay);
            newMDIChild4.Show();
        }

  
        private void passToDisplay(object sender, NewEventArgs e)
        {
            MessageDataReady(e);

        }


        private void aboutLindeCANToolToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CanToolAboutBox AboutBox = new CanToolAboutBox();
            AboutBox.Show();
        }

        
        private void SymbolsAdded_Click(object sender, EventArgs e)
        {
            if (!CurrentSymbols.SymbolsLoaded)
            {
                if (!CurrentSymbols.SymbolsInData)
                {
                    try
                    {
                        CurrentSymbols.ParseFile_Click(FileName);
                        LoadSymbols(sender,e);
                    }
                    catch
                    {
                        MessageBox.Show("Error:  Symbol File error");
                    }

                }
                else
                {
                    CurrentSymbols.SymbolsLoaded = true;
                    txRxWindowToolStripMenuItem_Click(this, e);
                    SymbolsAdded.Text = "Symbols Installed";
                    SymbolsAdded.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.ImageAndText;
                    SymbolsFileName.Text = CurrentSymbols.CurrentFileName;
                }
            }
            else
            {
                DialogResult result;

                result = MessageBox.Show("Symbol File Allready loaded.  Would you like to reload a new file?",
                                 "Load Error", MessageBoxButtons.YesNo,
                                 MessageBoxIcon.Information);
                
                if (result == DialogResult.Yes)
                {
                    CurrentSymbols.ParseFile_Click(FileName);
                    LoadSymbols(sender,e);
                }
            }
        }


        private void LoadSymbols(object TempSender,EventArgs e)
        {
            CurrentSymbols.SymbolsLoaded = true;
            txRxWindowToolStripMenuItem_Click(TempSender, e);
            SymbolsAdded.Text = "Symbols Installed";
            SymbolsAdded.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.ImageAndText;
            SymbolsFileName.Text = CurrentSymbols.CurrentFileName;
        }

        
        
        
        private void FindNodesClick(object sender, EventArgs e)
        {
            /* If network not connectt */

            if (Net.NetsNotConnected)
                return;

            /* Setup Display window   */

            NodeDisplay = new NetNodeFind(this);
            NodeDisplay.MdiParent = this;
            NodeDisplay.Show();

        }

        private void NodesConnect()
        {
            /* Setup Display window   */
            int hardware = GetHardware();

			if(hardware == 0) // PCAN
			{
	            connectToNet(this, EventArgs.Empty);
			}
			else
			{
	            connectToLindeCan(this, EventArgs.Empty); // Linde CAN
			}

        }


        private void broseForFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.InitialDirectory = CurrentPath + @"\Symbols";
            openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.FileName = SymbolFileName.Text;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                FileName = openFileDialog1.FileName;
                string[] pathname = FileName.Split(new Char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
                SymbolFileName.Text = pathname[pathname.Length - 1];
                //CurrentOptions.FileName = SymbolFileName.Text;
            }
        }


        private void ShowCaptureWindow(object sender, EventArgs e)
        {


        }
        private void MDIClosing(object sender, FormClosingEventArgs e)
        {
            ReadOptionsIni NewIni = new ReadOptionsIni();
            NewIni.WriteFile(ref CurrentOptions, CurrentPath);
        }

        private void macroWindowToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void SendTestSDO(object sender, EventArgs e)
        {
             SendSDO SendSDOWindow = SendSDO.MyInstance(this);

            SendSDOWindow.MdiParent = this;
            SendSDOWindow.NewData += new NewMessageHandler(passToDisplay);

            
            SendSDOWindow.Show();

        }

        private void flashSoftwareToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //if (!FlashSwWindow.Created)
            //if(FlashSwWindow == null)
            //{
            //    //FlashSwWindow = new FlashWindow(this);
            //    FlashSwWindow = new FlashWindow1319(this);
            //    FlashSwWindow.Show();
            //}
            //else if (!FlashSwWindow.Created)
            //{
            //    FlashSwWindow = new FlashWindow1319(this);
            //    FlashSwWindow.Show();

            //}
            //else
            //{
            //    FlashSwWindow.WindowState = FormWindowState.Normal;
            //    FlashSwWindow.Focus();
            //}
            
        }

        private void ResetHardware_Click(object sender, EventArgs e)
        {
            NetReset();
            if (newMDIChild1.OverFlowFault)
                newMDIChild1.OverFlowFault = false;
            newMDIChild1.SDOVarInitMDI();
        }

        private void MDIWindow_Load(object sender, EventArgs e)
        {

        }

		public uint GetBaudrate()
		{
			return (uint)CurrentOptions.BaudIndex;
		}
	    public void SetBaudrate(uint baud)
		{
			CurrentOptions.BaudIndex = (int)baud;
		}
		public string GetOSFileName()
		{
			return CurrentOptions.MainFileName;
		}
	    public void SetOSFileName(string name)
		{
			CurrentOptions.MainFileName = name;
		}

		public string GetMainNodeID()
		{
			return CurrentOptions.MainNodeID;
		}
	    public void SetMainNodeID(string node)
		{
			CurrentOptions.MainNodeID = node;
		}

        public void SetSN(string sn)
        {
            CurrentOptions.serial_number = sn;
        }

        public void SetSecurityKey(string key)
        {
            CurrentOptions.security_key = key;
        }



		public string GetPort()
		{
			return CurrentOptions.port;
		}
	    public void SetPort(string port)
		{
			CurrentOptions.port = port;
		}
		public int GetHardware()
		{
			return CurrentOptions.hardware;
		}
        public void SetHardware(int hardware)
		{
			CurrentOptions.hardware = hardware;
		}		
 
    }
}
